package com.wyr.leetcode.step1.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTreePathsTest {
    /**
     * 给你一个二叉树的根节点 root ，按 任意顺序 ，返回所有从根节点到叶子节点的路径。
     *
     * 叶子节点 是指没有子节点的节点。
     *
     * https://leetcode.cn/problems/binary-tree-paths/description/
     */
    static List<String> res = new ArrayList<>();
    public static List<String> binaryTreePaths(TreeNode root) {
        process(root,new LinkedList<Integer>());
        return res;
    }

    public static void process(TreeNode cur, LinkedList<Integer> tmpStr){
        if(cur == null){
            return;
        }
        if(cur.left==null && cur.right ==null){
            StringBuilder sb = new StringBuilder();
            for (Integer i : tmpStr) {
                sb.append(i).append("->");
            }
            sb.append(cur.val);
            res.add(sb.toString());
            return;
        }
        tmpStr.add(cur.val);
        process(cur.left, tmpStr);
        process(cur.right, tmpStr);
        tmpStr.removeLast();
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        binaryTreePaths(root);
    }
}
